package team.you.ticket.controller;

import cn.dsna.util.images.ValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.you.ticket.pojo.Result;
import team.you.ticket.pojo.User;
import team.you.ticket.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//被Springmvc扫描 表明这个类表示控制层
@Controller
//请求地址路径映射
@RequestMapping("/home")
public class UserController
{
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request)
    {
        User user=(User) request.getSession().getAttribute("user");//判断是否免密
        if (user!=null)
        {
            return "index";
        }
        else return "login";
    }
    @RequestMapping("/loginOut")
    private String loginOut(HttpServletRequest request)
    {
        request.getSession().invalidate();//清除session,忘记密码
        return "login";
    }
    @RequestMapping("/getCode")
    private void getCode(HttpServletRequest request, HttpServletResponse response)
    {
        //生成验证码
        ValidateCode validateCode=new ValidateCode(150,35,4,50);
        //获取验证码的验证信息 -后面登录时在服务层验证验证码正误
        String code = validateCode.getCode();
        //将code添加到session中
        request.getSession().setAttribute("code",code);
        //给session设置过期时间
        request.getSession().setMaxInactiveInterval(300);
        //通过输出流的方式，将验证码图片返回给前端
        try {
            validateCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/register")
    public String register()
    {
        return "register";
    }
    @RequestMapping("/upImg")
    @ResponseBody
    private Result upImg(MultipartFile file, HttpServletRequest request)
    {
        Result result=userService.upImg(file,request);
        return result;
    }
    //获取验证码
    @RequestMapping("/mobileCode")
    @ResponseBody
    public Result mobileCode(String phone,HttpServletRequest request){
        return userService.mobileCode(phone,request);
    }
    //验证码验证
    @RequestMapping("/codeVarify")
    @ResponseBody
    public Result codeVarify(String mobileCode, HttpServletRequest request){
        Result result = new Result();
        Integer code1 = (Integer) request.getSession().getAttribute("mobileCode");
        if(code1 != null){
            String code = code1.toString();
            if(mobileCode.equals(code)){
                return result;
            }else {
                result.setCode(500);
                result.setMsg("验证码过期或有误！");
                return result;
            }
        }else {
            result.setCode(500);
            result.setMsg("请先获取验证码！");
            return result;
        }
//        return result;
    }
    @ResponseBody
    @RequestMapping("/registerOn")
    private Result registerOn(User user)
    {
        return userService.register(user);
    }
    @RequestMapping("/login")
    @ResponseBody
    public Result login(String remember, String username, String password, String code, HttpServletRequest request)
    {
        return userService.login(remember,username,password,code,request);
    }
    @RequestMapping("/main")
    private String main()
    {
        return "index";
    }
    @RequestMapping("/userNameChange")
    @ResponseBody
    private Result userNameChange(String value)
    {
        return userService.userNameChange(value);
    }
    @RequestMapping("/returnLogin")
    private String returnLogin()
    {
        return "login";
    }
    @RequestMapping("/pwd")
    private String pwd()
    {
        return "pwdUpdate";
    }
    @RequestMapping("/msg")
    private String msg()
    {
        return "test";
    }
    @ResponseBody
    @RequestMapping("/getMsg")
    private Result getMsg()
    {
        return userService.getMsg();
    }
}
